import '../../util/_base_export.dart';
import '../../util/_view_export.dart';
import '../../blocs/menu.dart';

class CategoryItemComponent extends StatelessWidget {
  final String iv_name;
  final List iv_list;
  final int iv_category_id;

  // final GlobalKey iv_rootkey;
  // final Offset iv_offset;
  var lv_img = 'assets/order/category_default.png';

  CategoryItemComponent({
    Key? key,
    required this.iv_category_id,
    required this.iv_name,
    required this.iv_list,
  }) : super(key: key);

  Widget build(BuildContext context) {
    var color1;
    var color2;
    var image_url;
    var textColor;

    switch (iv_category_id.toString()) {
      case '1':
        {
          color1 = Color(0xFFD0F8BE);
          color2 = Color(0xFF01C792);
          image_url = 'assets/homepage/首页分类banner_水果蔬菜.png';
          textColor = Colors.white;
        }
        break;
      case '0':
        {
          color1 = Color(0xFFFF3434);
          color2 = Color(0xFFFF621C);
          image_url = 'assets/homepage/首页分类banner_限时抢购.png';
          textColor = Color(0xff000000);
        }
        break;
      case '2':
        {
          color1 = Color(0xFFFFAF88);
          color2 = Color(0xFFF4401F);
          image_url = 'assets/homepage/首页分类banner_肉禽蛋品.png';
          textColor = Color(0xff000000);
        }
        break;
      case '3':
        {
          color1 = Color(0xFFFF80C4);
          color2 = Color(0xFF6430F0);
          image_url = 'assets/homepage/首页分类banner_休闲零食2.png';
          textColor = Colors.white;
        }
        break;
      case '5':
        {
          color1 = Color(0xFF72EFFF);
          color2 = Color(0xFF149AFC);
          image_url = 'assets/homepage/首页分类banner_海鲜冻品.png';
          textColor = Colors.white;
        }
        break;
      case '6':
        {
          color1 = Color(0xFFFFCB55);
          color2 = Color(0xFFE38A49);
          image_url = 'assets/homepage/首页分类banner_粮油调味.png';
          textColor = Colors.black;
        }
        break;
      case '7':
        {
          color1 = Color(0xFFB9E1FF);
          color2 = Color(0xFFEBFBFF);
          image_url = 'assets/homepage/首页分类banner_牛奶饮品.png';
          textColor = Colors.white;
        }
        break;
      case '4':
        {
          color1 = Color(0xFFD3FFFA);
          color2 = Color(0xFF28C2CE);
          image_url = 'assets/homepage/首页分类banner_日杂百货.png';
          textColor = Colors.white;
        }
        break;
      case '8':
        {
          color1 = Color(0xFFC7F2FF);
          color2 = Color(0xFF78B4FA);
          image_url = 'assets/homepage/首页分类banner_专业干洗.png';
          textColor = Colors.white;
        }
        break;
      case '10':
        {
          color1 = Color(0xFFC7F2FF);
          color2 = Color(0xFF78B4FA);
          image_url = 'assets/homepage/首页分类banner_专业干洗.png';
          textColor = Colors.white;
        }
        break;
      case '11':
        {
          color1 = Color(0xFFFCD862);
          color2 = Color(0xFFFD8C2C);
          image_url = 'assets/homepage/首页分类banner_聚划算.png';
          textColor = Colors.white;
        }
        break;
      default:
        {
          //statements;
        }
        break;
    }
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        margin: EdgeInsets.only(bottom: 10.h),
        height: 287.h,
        // color: AppColor.blackMain,
        child:
            Stack(alignment: AlignmentDirectional.topCenter, children: <Widget>[
          Container(
              height: 244.h,
              margin: EdgeInsets.only(top: 43.h),
              width: 343.w,
              decoration: AppCard.homeCategoryBanner(color1, color2),
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 14.w),
                child: ListView.builder(
                    shrinkWrap: true,
                    scrollDirection: Axis.horizontal,
                    itemCount: iv_list.length,
                    itemBuilder: (BuildContext context, int index) =>
                        GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            App.router.navigateTo(
                                context,
                                AppRoutes.productDetail +
                                    '?prod_id=${iv_list[index]['sku_id']}');
                          },
                          child: Card(
                            margin: EdgeInsets.only(
                                top: 67.h, bottom: 27.h, right: 8.w),
                            child: Container(
                                height: 150.h,
                                width: 100.w,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Container(
                                      width: 80.w,
                                      height: 80.h,
                                      margin: EdgeInsets.only(bottom: 12.h),
                                      child:
                                          ((iv_list[index]['image_url'] ?? '')
                                                  .isEmpty)
                                              ? Image.asset(lv_img)
                                              : CachedNetworkImage(
                                                  imageUrl: iv_list[index]
                                                          ['image_url'] ??
                                                      '',
                                                  fit: BoxFit.fill,
                                                ),
                                    ),
                                    iv_category_id == 0
                                        ? AppTextWidget.originalPriceDelete(
                                            '\$' +
                                                iv_list[index]['origin_price'] +
                                                ' / ' +
                                                iv_list[index]['unit_desc'])
                                        : AppTextWidget.priceForSale(
                                            iv_list[index]['sku_name']),
                                    SizedBox(height: 3.h),
                                    AppTextWidget.priceForSale('\$' +
                                        iv_list[index]['sku_price'] +
                                        ' / ' +
                                        iv_list[index]['unit_desc']),
                                  ],
                                )),
                          ),
                        )),
              )),
          Container(
              width: 323.w,
              height: 86.h,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(image_url),
                  fit: BoxFit.cover,
                ),
                borderRadius: BorderRadius.all(Radius.circular(43)),
              ),
              child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () async {
                    if (iv_category_id == 0) {
                      App.router.navigateTo(context, AppRoutes.salesList);
                    } else {
                      var res =
                          await Provider.of<MenuBloc>(context, listen: false)
                              .fetchSubcategories(iv_category_id);
                      if (res['ev_error'] != 0) {
                        AppPopupService.showText(res['ev_context']);
                      } else {
                        App.router
                            .navigateTo(context, AppRoutes.subCategoryList);
                      }
                    }
                  },
                  child: Container(
                      padding: EdgeInsets.only(left: 158.w, top: 14.h),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          AppTextWidget.categoryBannerText(iv_name, textColor),
                          SizedBox(height: 6.h),
                          Row(
                            children: [
                              AppTextWidget.categoryBannerMoreText(
                                  '点击查看全部', textColor),
                              SizedBox(width: 3.w),
                              Icon(
                                Icons.chevron_right,
                                color: Colors.white,
                                size: 16,
                              ),
                            ],
                          )
                        ],
                      ))))
        ]));
  }
}
